var controllers = angular.module('controllers', []);

controllers.run(function ($window, $rootScope, $location, $timeout, $http, AlertService) {
    $rootScope.location = $location;
    $rootScope.loadingModelControl = {};
    $rootScope._processError = function (e) {
        AlertService.addAlert({type: 'danger', msg: '系统异常! '}, 3000);
    };
    $rootScope.ajaxRequest = function (requestUrl, successCallback) {
        setTimeout(function () {
            $rootScope.loadingModelControl.showLoading();
            $http.get(requestUrl).success(function (resultJson) {
                $rootScope.loadingModelControl.hideLoading();
                if (!resultJson.success) {
                    //登陆验证
                    if (resultJson.errorCode == 10002) {
                        AlertService.addAlert({type: 'danger', msg: '未登录，请先登陆！！'}, 3000);
                        $rootScope.user = {
                            isLogin: false,
                            userName: ''
                        };
                    } else if (resultJson.errorMsg == "has not privilege") {
                        AlertService.addAlert({type: 'danger', msg: '没有权限啦！请申请权限'}, 3000);
                    } else {
                        AlertService.addAlert({type: 'danger', msg: '发生错误了! ' + resultJson.errorMsg}, 2000);
                    }
                } else {
                    successCallback(resultJson);
                }
            }).error(function (e) {
                $rootScope._processError(e);
            });
        }, 200);
    };
    $rootScope.currentPageName = "运营工作台";
    $rootScope.resetCurrentPageName = function (pageName) {
        $rootScope.currentPageName = pageName;
    };

    $rootScope.ajaxRequestPost = function (requestUrl, data, successCallback, failCallBack) {
        setTimeout(function () {
            $rootScope.loadingModelControl.showLoading();
            var transFn = function (data) {
                    return $.param(data);
                },
                postCfg = {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: transFn
                };
            $http.post(requestUrl, data, postCfg).success(function (resultJson) {
                $rootScope.loadingModelControl.hideLoading();
                if (!resultJson.success) {
                    if (resultJson.errorCode == 10002) {
                        $rootScope.user = {
                            isLogin: false,
                            userName: ''
                        };
                        AlertService.addAlert({type: 'danger', msg: '未登录，请先登陆！！'}, 3000);
                    } else {
                        AlertService.addAlert({type: 'danger', msg: '发生错误了! ' + resultJson.errorMsg}, 3000);
                        if (failCallBack) {
                            failCallBack();
                        }
                    }
                } else {
                    successCallback(resultJson);
                }
            }).error(function (e) {
                $rootScope._processError(e);
            });
        }, 200);
    };

    //用户相关信息
    $rootScope.user = {
        isLogin: false
    };
    $rootScope.login = function () {
        $rootScope.ajaxRequestPost("/azeroth/back/login/login.htm",
            {
                memberName: $rootScope.user.userName,
                memberPwd: $rootScope.user.password
            },
            function (resultJson) {
                $rootScope.user = {
                    isLogin: true,
                    userName: resultJson.data.memberName
                };
            }, null);
    };
    $rootScope.logout = function () {
        $rootScope.ajaxRequestPost("/azeroth/back/login/logout.htm",
            {},
            function (resultJson) {
                $rootScope.user = {
                    isLogin: false,
                    userName: ''
                };
            }, null);
    };
    $rootScope.getUserInfo = function () {
        $rootScope.ajaxRequestPost("/azeroth/back/login/getUserInfo.htm", {}, function (resultJson) {
            if (resultJson.data) {
                $rootScope.user = {
                    isLogin: true,
                    userName: resultJson.data.memberName
                };
            } else {
                $rootScope.user = {
                    isLogin: false,
                    userName: ""
                };
            }

        }, null);
    };
    $rootScope.getUserInfo();

});

//首页
controllers.controller('indexChildCtrl', ['$scope', '$http', '$rootScope', '$modal', 'AlertService', function ($scope, $http, $rootScope, $modal, AlertService) {
    $scope.resetCurrentPageName("运营工作台");
}]);
